import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {
    public static void main(String[] args) {
        int port = 8888;
        System.out.println("等待客户端连接………………");
        try (ServerSocket serverSocket = new ServerSocket(port);) {
            Socket socket = serverSocket.accept();
            System.out.println("客户端连接: " + socket.getInetAddress());
            InputStream inputStream = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String message;
            while ((message = reader.readLine()) != null) { // 按行读取
                System.out.println("收到消息: " + message);
            }
        } catch (IOException e) {
            System.out.println("客户端断开连接");
            e.printStackTrace();
        }
        System.out.println("客户端主动断开");
    }
}